package org.dragonnova.meetingapp.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


import org.dragonnova.meetingapp.R;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
    }

    private void initView() {
        ViewPager guide_vp_guide = (ViewPager) findViewById(R.id.guide_vp_guide);
        Button guide_bt_login = (Button) findViewById(R.id.guide_bt_login);
        Button guide_bt_register = (Button) findViewById(R.id.guide_bt_register);

        guide_bt_login.setOnClickListener(this);
        guide_bt_register.setOnClickListener(this);

        ArrayList<View> viewList = new ArrayList<>();
        View view1 = View.inflate(this, R.layout.item_guide, null);
        View view2 = View.inflate(this, R.layout.item_guide, null);
        View view3 = View.inflate(this, R.layout.item_guide, null);
        View view4 = View.inflate(this, R.layout.item_guide, null);
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);
        PagerAdapter pagerAdapter = new ViewPagerAdapter(viewList);
        guide_vp_guide.setAdapter(pagerAdapter);
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.guide_bt_login:
                break;
            case R.id.guide_bt_register:
                Intent intent = new Intent(this, MainActivity1.class);
                startActivity(intent);
                break;
        }
    }

    private class ViewPagerAdapter extends PagerAdapter {
        private List<View> mListViews;

        public ViewPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;//构造方法，参数是我们的页卡，这样比较方便。
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mListViews.get(position));//删除页卡
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {    //这个方法用来实例化页卡
            container.addView(mListViews.get(position), 0);//添加页卡
            return mListViews.get(position);
        }

        @Override
        public int getCount() {
            return mListViews.size();//返回页卡的数量
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;//官方提示这样写
        }
    }
}
